/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.navid.trafalgar.mod.counterclock.statelisteners;

import com.jme3.app.Application;
import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.post.Filter;
import com.jme3.post.FilterPostProcessor;
import com.jme3.scene.Node;
import com.navid.trafalgar.definition.GameDefinition;
import com.navid.trafalgar.manager.EventManager;
import com.navid.trafalgar.manager.states.LoadModelState;
import com.navid.trafalgar.manager.GameConfiguration;
import com.navid.trafalgar.manager.GameStatus;
import com.navid.trafalgar.model.AMillestoneModel;
import com.navid.trafalgar.model.AShipModel;
import com.navid.trafalgar.model.GameModel;
import com.navid.trafalgar.model.IContext;
import java.util.Map;

/**
 *
 * @author anf
 */
public class LoadMapStateListener implements LoadModelState {

    AssetManager assetManager;
    InputManager inputManager;
    GameStatus gameStatus;
    GameConfiguration gameConfiguration;
    EventManager eventManager;

    public LoadMapStateListener(Application app, EventManager eventManager, GameStatus gameStatus, GameConfiguration gameConfiguration) {
        this.assetManager = app.getAssetManager();
        this.inputManager = app.getInputManager();
        this.gameConfiguration = gameConfiguration;
        this.gameStatus = gameStatus;
        this.eventManager = eventManager;
    }

    public void onLoadModel(float tpf) {

        GameDefinition gameDefinition = (GameDefinition) assetManager.loadAsset(gameConfiguration.getMap());
        gameStatus.setGameDefinition(gameDefinition);

        GameModel gameModel = gameConfiguration.getModelBuilder().buildGameModel(gameDefinition);
        gameStatus.setGameModel(gameModel);

        IContext iContext = gameModel.getiContext();
        gameStatus.getGameNode().attachChild(iContext.getWind().getGeometry());

        Map<String, AShipModel> ships = gameModel.getShips();
        for (AShipModel currentShip : ships.values()) {
            currentShip.registerInput(inputManager);
            gameStatus.getGameNode().addControl(currentShip);
        }

        Map<String, AMillestoneModel> millestones = gameModel.getMillestones();
        for (AMillestoneModel currentMillestone : millestones.values()) {
            currentMillestone.setEventManager(eventManager);
            currentMillestone.setCollidable(ships.values());
            gameStatus.getGameNode().addControl(currentMillestone);
        }

        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
        for (Filter currentFilter : gameModel.getFpp()) {
            fpp.addFilter(currentFilter);
        }

        gameStatus.getViewPort().addProcessor(fpp);
        gameStatus.getGameNode().attachChild(gameModel.getGameNode());
    }

    public void onUnload() {
        
        gameStatus.getGameNode().removeControl(gameStatus.getMillestoneGameModel().getShip());

        Map<String, AMillestoneModel> millestones = gameStatus.getMillestoneGameModel().getMillestones();
        for (AMillestoneModel currentMillestone : millestones.values()) {
            gameStatus.getGameNode().removeControl(currentMillestone);
        }
        gameStatus.getGameNode().detachAllChildren();
        gameStatus.setGameDefinition(null);
        gameStatus.setGameModel(null);

    }
}
